<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <span id="titleVal"></span>
    </header>
    <!-- <div class="aui-content aui-margin-b-15" style="margin-top:50px;"> -->
    <!-- </br> -->
    <!-- //radio -->
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">扫码模式 </div>
                    <div class="aui-list-item-input">
                        <label><input class="aui-radio" type="radio" value="1" name="operateType" checked>垛码</label>
                        <label><input class="aui-radio" type="radio" value="2" name="operateType"> 总成码</label>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">类型 </div>
                    <div class="aui-list-item-input">
                        <label><input class="aui-radio" type="radio" value="bind" name="type" checked>绑定</label>
                        <label><input class="aui-radio" type="radio" value="replace" name="type">替换</label>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">二维码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描/输入 二维码" id="consignment" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <!-- <div class="aui-btn aui-btn-info aui-btn-block" style="width:60%;margin-left:20%;font-size: 12px;font-weight: bold;" onclick="createUqcmChkBill()">一键确认</div> -->
    <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:50px;">
        <table class="table" style='width:100%;' id="table">
            <thead>
                <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                    <td algin='center'></td>
                    <td algin='center'>vin</td>
                    <td algin='center'>发货顺序号</td>
                    <td algin='center'>气门嘴码</td>
                    <td algin='center'>气门嘴顺序号</td>
                    <td algin='center'>车型号</td>
                    <td algin='center'>发货状态</td>
                    <td style="display:none;">id</td>
                </tr>
            </thead>
            <tbody id="tbodyHtml">
            </tbody>
        </table>
    </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    // var idSelector = "";

    //初始化查询仓库下拉框
    apiready = function() {
        $("#consignment").select();
        $api.html($api.byId("titleVal"), api.pageParam.code);
        // $("#consignment").focus();
        // idSelector = "consignment";
        searchDetails(api.pageParam.code);
    }

    // // 获取光标所在组件的id
    // function getIdSelector() {
    //     var e = event || window.event;
    //     var el = e.srcElement || e.target;
    //     idSelector = el.id;
    //     $("#" + el.id).select();
    // }

    //一键发货
    function createUqcmChkBill() {
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function() {
            count++;
        });
        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            return;
        }
        var list = [];

        check.each(function() { //遍历
            var row = $(this).parent("td").parent("tr");
            var sysid = row.find("[name='sysid']").html();
            var vin = row.find("[name='vin']").html();

            list.push({
                "sysid": sysid,
                "vin": vin
            });
        });

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/confirDeliveryWheelOnline";
        var data = {
            values: {
                list: JSON.stringify(list),
                appVersion: api.appVersion,
                un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    searchDetails();
                    api.toast({
                        msg: '发货确认成功!',
                        duration: 3000
                    });

                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });


    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            //1：二维码
            if (idSelector == 'consignment') {

                //二维码
                $("#consignment").val(ret);

                //查询表格数据
                scanQrCode(ret);

            }
        });
    }

    //发货单监听回车事件
    $("#consignment").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var consignment = $(this).val();
        if (keycode == '13') {

            //发货单
            $("#consignment").val(consignment);

            //查询表格数据
            scanQrCode(consignment);
        }
    });

    function scanQrCode(consignment) {
        var type = $("input[name='type']:checked").val();

        //绑定
        if (type == "bind") {
            bind(consignment);

            //解绑
        }else if(type == "replace"){

          //选中数据
          var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
          var count = 0;
          check.each(function() {
              count++;
          });

          //选中行不为1
          if (count != 1) {
              api.toast({
                  msg: '请选择一条表格数据！',
                  duration: 3000
              });
              return;
          }

          //id
          var sysid = "";
          //气门嘴码
          var ext08 = "";
          // var purchaseOrder = "";
          check.each(function() { //遍历
              var row = $(this).parent("td").parent("tr");
              sysid = row.find("[name='sysid']").html(); //注意html()和val()
              ext08 = row.find("[name='ext08']").html();
          });

          if(ext08 == null || ext08 =="" || ext08 ==undefined){
            api.toast({
                msg: '该条数据还未绑定气门嘴码，不允许解绑！',
                duration: 3000
            });
            return;
          }

          replace(consignment,sysid);
        }
    }

    function replace(consignment,sysid){
      var un = localStorage.getItem('un');
      var serverUrl = localStorage.getItem('serverUrl');
      var carno = document.getElementById("titleVal").innerHTML;
      var url = serverUrl + "/app/scanQrCode";
      var operateType = $("input[name='operateType']:checked").val();
      var type = $("input[name='type']:checked").val();
      var data = {
          values: { ////以表单方式提交参数（JSON对象）
              un: un,
              code: consignment,
              carno: carno,
              operateType: operateType,
              type:type,
              appVersion: api.appVersion,
              sysid:sysid
          }
      };

      api.ajax({
          url: url,
          method: 'post',
          dataType: 'json',
          timeout: 30,
          returnAll: false,
          data: data
      }, function(ret, err) {
          if (ret) {
              var data = JSON.stringify(ret);
              var myobj = eval("(" + data + ")");
              //返回成功
              if (myobj.code == "0") {
                  searchDetails(carno);
                  document.getElementById("consignment").innerHTML = "";
                  $("#consignment").select();
                  api.toast({
                      msg: myobj.message,
                      duration: 3000
                  });
                  //失败
              } else if (myobj.code == "1") {

                  document.getElementById("consignment").innerHTML = "";
                  $("#consignment").select();
                  idSelector = "consignment";
                  api.toast({
                      msg: myobj.message,
                      duration: 3000
                  });
              }
          }
      });
    }
    // //发货单号值改变
    // function onChangeConsignment() {
    //     //取发货单号
    //     var consignment = $("#consignment").val();
    //     //发货单为空，清空表格信息
    //     if (consignment == "" || consignment == null || consignment == undefined) {
    //
    //         $api.html($api.byId("tbodyHtml"), "");
    //
    //         //光标
    //         $("#consignment").focus();
    //         idSelector = "consignment";
    //
    //         //不为空，
    //     } else {
    //
    //         //查询表格数据
    //         searchDetails(consignment);
    //     }
    // }\

    function bind(consignment) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var carno = document.getElementById("titleVal").innerHTML;
        var url = serverUrl + "/app/scanQrCode";
        var operateType = $("input[name='operateType']:checked").val();
        var type = $("input[name='type']:checked").val();
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                code: consignment,
                carno: carno,
                operateType: operateType,
                appVersion: api.appVersion,
                type:type
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    searchDetails(carno);
                    document.getElementById("consignment").innerHTML = "";
                    $("#consignment").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    //失败
                } else if (myobj.code == "1") {

                    document.getElementById("consignment").innerHTML = "";
                    $("#consignment").select();
                    idSelector = "consignment";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //根据发货单查询表格数据
    function searchDetails(code) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/deliveryWheelOnline";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un,
                appVersion: api.appVersion,
                code: code
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {

                    var list = myobj.list;
                    var html = "";

                    var color = ['#F5F5F5', '#DCDCDC'];
                    var index = 0;
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        if (i == 0) {
                            html += "<tr style='text-align: center;background-color: " + color[index] + ";color: black;font-size: 12px;'>";
                        } else {
                            if (list[i].vin != list[i - 1].vin) {
                                if (index == 0) {
                                    index = 1;
                                } else {
                                    index = 0;
                                }
                            }
                            html += "<tr style='text-align: center;background-color: " + color[index] + ";color: black;font-size: 12px;'>";
                        }

                        html += "<td ><input name='sub' type='checkbox' /></td>";
                        html += "<td name='vin' >" + list[i].vin + "</td>";
                        html += "<td name='ext06' >" + list[i].ext06 + "</td>";
                        if (list[i].ext08 == null || list[i].ext08 == undefined) {
                            html += "<td name='ext08' ></td>";
                        } else {
                            html += "<td name='ext08' >" + list[i].ext08 + "</td>";
                        }
                        if (list[i].ext05 == null || list[i].ext05 == undefined) {
                            html += "<td name='ext05' ></td>";
                        } else {
                            html += "<td name='ext05' >" + list[i].ext05 + "</td>";
                        }
                        html += "<td name='sub_mrlcode' >" + list[i].sub_mrlcode + "</td>";
                        if (list[i].ext11 == 'Y') {
                            html += "<td name='ext11' style='background-color:#228B22;'>" + list[i].ext11 + "</td>";
                        } else {
                            html += "<td name='ext11'>" + list[i].ext11 + "</td>";
                        }
                        html += "<td name='sysid' style='display:none;'>" + list[i].sysid + "</td>";
                        html += "</tr>";
                    }

                    $api.html($api.byId("tbodyHtml"), html);

                    //失败
                } else if (myobj.code == "1") {

                    $("#consignment").select();
                    idSelector = "consignment";
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    // //页面刷新
    // function refresh(consignment) {
    //     searchDetails(consignment);
    // }


    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
